<?php
declare (strict_types = 1);

namespace app\admin\service\market;

use app\admin\logic\market\VipConfig as VipConfigLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\facade\Request;

/**
 * vip配置
 */
class VipConfig extends BaseService
{
    /**
     * 配置列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     * @return   [type]                         [description]
     */
    public function index()
    {
        $data = app(VipConfigLogic::class)->getList([]);

        // 定义字段
        $keyList = [
            'title'       => ListItem::make()->title('名称'),
            'days'        => ListItem::make()->title('有效期（天）'),
            'price'       => ListItem::make('custom')->title('售价'),
            'give_credit' => ListItem::make()->title('赠送积分'),
            'give_days'   => ListItem::make()->title('赠送天数'),
            'update_time' => ListItem::make()->title('最后更新时间'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        // $list->searchKey = $searchKey;
        $list->datas = $data;

        return $list->send();
    }

    /**
     * 新建会员配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     * @return   [type]                         [description]
     */
    public function create()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $res  = app(VipConfigLogic::class)->createVipConfig($data);
            return $this->parseToData($res->toArray(), 1, '添加成功');
        }
        $keyList = [
            'title'          => FormItem::make()->title('名称')->help('如月卡、季卡、年卡等')->maxlength(20)->required(),
            'days'           => FormItem::make('inputNumber')->title('有效期（天）')->min(1)->required(),
            'scribing_price' => FormItem::make('inputNumber')->title('原价')->min(0)->precision(2),
            'price'          => FormItem::make('inputNumber')->title('售价')->min(0)->precision(2)->required(),
            'give_credit'    => FormItem::make('inputNumber')->title('赠送积分')->min(0)->help('购买成功后,可获得积分'),
            'give_days'      => FormItem::make('inputNumber')->title('赠送天数')->min(0)->help('购买成功后,额外赠送会员天数'),
        ];

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey    = $keyList;
        $form->saveMethod = 'POST';

        $form->savePostUrl = '/admin/market/vip/create';

        return $form->send();
    }

    /**
     * 编辑会员配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     * @param    int                            $id [description]
     * @return   [type]                             [description]
     */
    public function edit(int $id)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $res  = app(VipConfigLogic::class)->editVipConfig($id, $data);
            return $this->parseToData($res->toArray(), 1, '编辑成功');
        }

        $data = app(VipConfigLogic::class)->where('id', $id)->findOrFail();

        $keyList = [
            'title'          => FormItem::make()->title('名称')->help('如月卡、季卡、年卡等')->maxlength(20)->required(),
            'days'           => FormItem::make('inputNumber')->title('有效期（天）')->min(1)->required(),
            'scribing_price' => FormItem::make('inputNumber')->title('原价')->min(0)->precision(2),
            'price'          => FormItem::make('inputNumber')->title('售价')->min(0)->precision(2)->required(),
            'give_credit'    => FormItem::make('inputNumber')->title('赠送积分')->min(0)->help('购买成功后,可获得积分'),
            'give_days'      => FormItem::make('inputNumber')->title('赠送天数')->min(0)->help('购买成功后,额外赠送会员天数'),
        ];

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey     = $keyList;
        $form->datas       = $data;
        $form->saveMethod  = 'PUT';

        $form->savePostUrl = '/admin/market/vip/' . $id . '/edit';

        return $form->send();
    }

    /**
     * 删除会员配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-14
     * @param    int                            $id [description]
     * @return   [type]                             [description]
     */
    public function delete(int $id)
    {
        $res = app(VipConfigLogic::class)->deleteVipConfig($id);

        return $this->parseToData($res, 1, '删除成功');
    }
}
